<template>
  <div class="div">  
    <h1>在线教育管理系统</h1>
    <div class="box">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
          <el-input v-model="ruleForm.password2" type="password"></el-input>
        </el-form-item>
        <el-form-item label="身份">
          <el-select v-model="ruleForm.identity" placeholder="请选择身份">
            <el-option label="管理员" value="管理员"></el-option>
            <el-option label="小魔仙" value="小魔仙"></el-option>
            <el-option label="我wang总" value="我wang总"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width: 100%"
            @click="submitForm('ruleForm')"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    // 回调函数  写箭头函数 可以拿到组件实例
    var validatePassword2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        name:"",
        email:"",
        password:"",
        password2:"",
        identity:""
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入用户名",
          },
          { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱", 
          },
            { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
        ],
        password: [
          {
            validator: validatePassword, 
            trigger: ["blur", "change"],
          },
        ],
        password2: [
          {
            validator: validatePassword2, 
            trigger: ["blur", "change"],
          },
          ],
        identity: [
          {
            required: true,
            message: "请选择身份",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          var res = await this.$http({
            url: "/v1/vue3system/api/users/register",
            method: "post",
            data: this.ruleForm,
          });
          console.log(res);
          if (res.status == 200) {
            this.$message("注册成功,请登录");
          }

          this.$router.push({ name: "login" });
          this.ruleForm = {
            name: "",
            email: "",
            password: "",
            password2: "",
            identity: "",
          };
          return;
        } else if (this.password != this.password2) {
          this.$message("两次密码不一致");
        } else {
          this.$message("请补全信息");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.div {
  width: 100%;
  height: 100%;
  background: rgb(45, 58, 75);
  h1 {
    color: white;
    text-align: center;
    padding-top: 50px;
  }
  .box {
    width: 400px;
    height: 500px;
    color: white;
    padding-left: 33%;
    padding-top: 20px;
  }
}
</style>